package com.example.pets.model.datasource.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.pets.model.datasource.UserDataSource;
import com.example.pets.model.datasource.DataSourceImpl.UserDataSourceImpl;
import com.example.pets.model.entities.User;

public class AuthViewModel extends ViewModel {
    private final UserDataSource userDataSource;
    private final MutableLiveData<User> userLiveData = new MutableLiveData<>();
    private final MutableLiveData<String> errorLiveData = new MutableLiveData<>();

    public AuthViewModel() {
        userDataSource = new UserDataSourceImpl();
    }

    public LiveData<User> getUserLiveData() {
        return userLiveData;
    }

    public LiveData<String> getErrorLiveData() {
        return errorLiveData;
    }

    public void login(String email, String password) {
        userDataSource.login(email, password, new UserDataSource.AuthCallback() {
            @Override
            public void onSuccess(User user) {
                userLiveData.postValue(user);
            }

            @Override
            public void onFailure(String errorMessage) {
                errorLiveData.postValue(errorMessage);
            }
        });
    }

    public void register(String email, String password) {
        userDataSource.register(email, password, new UserDataSource.AuthCallback() {
            @Override
            public void onSuccess(User user) {
                userLiveData.postValue(user);
            }

            @Override
            public void onFailure(String errorMessage) {
                errorLiveData.postValue(errorMessage);
            }
        });
    }
}
